package com.flyee.utils
{
	import com.flyee.events.LoaderEvent;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	
	public class IconLoader extends Loader
	{
        private var iconID:String = "";
        private var iconUrl:String = null;
		public function IconLoader(url:String,iconID:String = "")
		{
			this.iconID = iconID;
			this.iconUrl = url;
            var request:URLRequest = new URLRequest(url);
            var loadContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain,SecurityDomain.currentDomain);
            configureListeners();
            try {
                this.load(request,loadContext);
                //this.loadBytes(
            } catch (error:Error) {
                MyLog.error("Unable to load requested URL.");
            }
		}
	
        private function configureListeners():void {
            this.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            this.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            this.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);
            this.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            this.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        }
        
        private function removeListeners():void{
        	this.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
            this.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            this.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            this.contentLoaderInfo.removeEventListener(Event.OPEN, openHandler);
            this.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
            this.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        }

        private function completeHandler(event:Event):void {
            var load:Loader = Loader(event.currentTarget.loader);
           // var byteArray:ByteArray = load.contentLoaderInfo.bytes;
           // byteArray.position = 0;
        	var bitmap:Bitmap = load.content as Bitmap;
         	this.dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE_LOADER,bitmap));
        }

        private function openHandler(event:Event):void {
           // trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
           // trace("progressHandler: " + event.bytesLoaded + "::" + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
        	var bitmap:Bitmap = new Bitmap();
        	//bitmap.bitmapData = PlatformDepot.getInstance().getBitmapDataByName("TempIcon");
        	this.dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE_LOADER,bitmap));
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
           // trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
	      	var bitmap:Bitmap = new Bitmap();
        	//bitmap.bitmapData = PlatformDepot.getInstance().getBitmapDataByName("TempIcon");
        	this.dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE_LOADER,bitmap));
        }
        public function getIconID():String
        {
        	return this.iconID;
        }
        public function clear():void{
           this.removeListeners();
           this.unload(); 
        }
	}
}